package com.tengju.data.infrastructure.repository.clickhouse.activity;

import com.tengju.data.domain.model.activity.*;
import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.clickhouse.activity.ActivityTargetAnalysisMapper;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.clickhouse.activity.ActivityTargetAnalysisTmpMapper;
import com.tengju.user.domain.model.user.UserIdCode;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/10/14
 */
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Repository
public class ActivityTargetAnalysisRepositoryImpl implements ActivityTargetAnalysisRepository {

    private final ActivityTargetAnalysisMapper activityTargetAnalysisMapper;
    private final ActivityTargetAnalysisTmpMapper tmpMapper;

    @Override
    public List<Row> dailyOverview(ActivityAnalysisQuery activityAnalysisQuery) {
        List<Map<String,Object>> result = activityTargetAnalysisMapper.dailyOverview(activityAnalysisQuery);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> dailyChart(ActivityAnalysisQuery activityAnalysisQuery) {
        List<Map<String,Object>> result = activityTargetAnalysisMapper.dailyChart(activityAnalysisQuery);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public LocalDateTime dataUpdateTime(ActivityAnalysisQuery query) {
        Date dataUpdateTime = activityTargetAnalysisMapper.getDataUpdateTime(query);
        return DateUtil.dateToLocalDateTime(dataUpdateTime);
    }

    @Override
    public Row getActivityGmvCntTotal(List<UserIdCode> genSecIdCodeList, ActivityId activityId, KanbanType kanbanType) {
        if (activityId == null){
            return null;
        }
        List<Long> genSecIdCodes = genSecIdCodeList.stream().map(UserIdCode::getValue).collect(Collectors.toList());
        Map<String,Object> res = activityTargetAnalysisMapper.getActivityGmvCntTotal(genSecIdCodes, activityId.getValue(),kanbanType.getVal());
        if (CollectionUtils.isEmpty(res)) {
            return null;
        }
        return new Row(res);
    }

    @Override
    public List<Row> userTargetReach(ActivityViewReachQuery activityViewReachQuery) {
        List<Map<String,Object>> result = activityTargetAnalysisMapper.userTargetReach(activityViewReachQuery);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> chiefTargetReach(ActivityViewReachQuery activityViewReachQuery) {
        List<Map<String, Object>> result = activityTargetAnalysisMapper.chiefTargetReach(activityViewReachQuery);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public int chiefTargetReachTotal(ActivityViewReachQuery activityViewReachQuery) {
        return activityTargetAnalysisMapper.chiefTargetReachCount(activityViewReachQuery);
    }

    @Override
    public List<Row> getChartData(ActivityViewReachQuery activityViewReachQuery) {
        List<Map<String,Object>> result = activityTargetAnalysisMapper.getChartData(activityViewReachQuery);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public int userTargetReachTotal(ActivityViewReachQuery activityViewReachQuery) {
        return activityTargetAnalysisMapper.userTargetReachTotal(activityViewReachQuery);
    }

    @Override
    public List<Row> getDimInfoList(ActivityViewReachQuery query) {
        List<Map<String, Object>> result = activityTargetAnalysisMapper.getDimInfo(query);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> saleTarget(ActivityAnalysisQuery query) {
        List<Map<String, Object>> result = activityTargetAnalysisMapper.saleTarget(query);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public Row saleOverview(ActivityAnalysisQuery query) {
        List<Map<String, Object>> result = activityTargetAnalysisMapper.saleOverview(query);
        if (CollectionUtils.isEmpty(result) || result.get(0) == null) {
            return new Row(new HashMap<>());
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList()).get(0);
    }

    @Override
    public List<Row> pullOverview(ActivityViewReachQuery query) {
        List<Map<String, Object>> result = activityTargetAnalysisMapper.pullOverview(query);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public String getDimName(ActivityViewReachQuery activityAnalysisQuery) {
        return activityTargetAnalysisMapper.getDimName(activityAnalysisQuery);
    }
}
